package com.ys.everydaynews.ui.main.activityy

import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.util.Log
import android.widget.Toast
import com.ys.everydaynews.R
import com.ys.everydaynews.base.BaseActivity
import com.ys.everydaynews.base.Constants.COUNT_TIME
import com.ys.everydaynews.base.Constants.COUNT_TIME_INTERVAL
import com.ys.everydaynews.base.Constants.MOBLIE
import com.ys.everydaynews.base.Constants.TOKEN
import com.ys.everydaynews.databinding.LayoutSendcodeBinding
import com.ys.everydaynews.mvp.model.BaseModel
import com.ys.everydaynews.ui.main.model.CodeModel
import com.ys.everydaynews.ui.main.model.EventModel
import com.ys.everydaynews.ui.main.presenter.SendCodePresenter
import com.ys.everydaynews.ui.main.view.SendCodeView
import com.ys.everydaynews.utils.*

class SendCodeActivity : BaseActivity<SendCodeView, SendCodePresenter, LayoutSendcodeBinding>(),
    CountDownLitener, SendCodeView ,TextWatcher {

    private var mAuthCodeTimer: AuthCodeTimer? = null
    private var phoneNumber: String? = null
    private var code: String? = null
    private var codeInput: String? = null

    override fun getLayoutId(): Int = R.layout.layout_sendcode
    override fun isRegister(): Boolean =true
    override fun initView() {
        if (intent != null) {
            phoneNumber = intent.getStringExtra(MOBLIE).toString()
        }

        binding.tvShowMoblie.text = "验证码已发送到$phoneNumber"

        binding.btnBack.setOnClickListener { finish() }
        binding.tvUpdataMoblie.setOnClickListener { finish() }
        binding.tvSendCode.setOnClickListener { getHttpSendCode() }
        binding.cdNext.setOnClickListener {
            if (TextUtils.isEmpty(codeInput)){
                "请输入验证码".toast(this)
                return@setOnClickListener
            }
            if (codeInput==code){
                getPersenter()?.postCode(phoneNumber!!)
            }else{
                "验证码错误".toast(this)
            }
        }
    }

    override fun initData() {
        getHttpSendCode()
    }

    fun getHttpSendCode() {
        if (!TextUtils.isEmpty(phoneNumber.toString())) {
            //获取验证码比较
            //  getPersenter()!!.getCode(CommonUtils.replaceBlank(phoneNumber!!))
            mAuthCodeTimer = AuthCodeTimer(COUNT_TIME, COUNT_TIME_INTERVAL, this)
            mAuthCodeTimer?.start()
        }
    }

    override fun createPersenter(): SendCodePresenter = SendCodePresenter()

    override fun <T> setData(data: T) {
        if (data != null) {
            mAuthCodeTimer = AuthCodeTimer(COUNT_TIME, COUNT_TIME_INTERVAL, this)
            mAuthCodeTimer?.start()
            val codeModel: CodeModel = data as CodeModel
            code = codeModel.verificationCode
        }
        Log.e("data", data.toString())
    }

    override fun setError(err: String) {
        Log.e("err", err)
    }

    override fun setRegisterData(data: BaseModel<CodeModel>) {
        if (data!=null){
            //保存Token
            SharedPreferencesUtils.saveToken(this,TOKEN, data.code.toString())
            //发送消息关闭上页
            postMsg(EventModel(101,data.code))

            finish()
        }
    }

    override fun setRegisterError(err: String) {

    }

    override fun countDown(time: Long) {
        val s = "重新获取(${time / 1000})秒"
        binding.tvSendCode.text = s
        binding.tvSendCode.isEnabled = false
    }

    override fun isOverTime(isOverTime: Boolean) {
        val s = "重新获取"
        binding.tvSendCode.text = s
        binding.tvSendCode.isEnabled = true
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        codeInput = s.toString()
        if (codeInput!!.length == 4) {
            if (codeInput == code) {
                Toast.makeText(this, "验证码正确！", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "验证码错误！", Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun afterTextChanged(s: Editable?) {

    }

}